package myJs.detailInfo

import japgolly.scalajs.react.ScalaFnComponent
import japgolly.scalajs.react.vdom.all._
import myJs.cps.BootstrapTableCp
import myJs.cps.BootstrapTableCp._
import myJs.myPkg.Hooks._
import myJs.myPkg.tabs._
import myJs.Implicits._
import myJs.api.Api

import scala.collection.immutable.SeqMap

/** Created by yz on 28/11/2023
  */
object SeqCp {

  case class Props(id: String)

  val Component = ScalaFnComponent[Props] { props =>
    val (datas, setDatas) = useState(List.empty[ujson.Obj])
    val (columns, setColumns) = useState(List.empty[Column])

    object FTool {

      def tbFmt(field: String) = (v: ujson.Value, row: ujson.Obj) => {
        field match {
          case _ => span(v.myStr)()
        }
      }

      def comVFmt(fileTitle: String) = (v: ujson.Value, row: ujson.Obj) => {
        fileTitle match {
          case _ => v.myStr
        }
      }

      def operateFmt = (v: ujson.Value, row: ujson.Obj) => {
            span(
              a(
                cursor.pointer,
                marginBottom := 5.px,
                title := "Download",
                href:=s"${Api.browse.downloadMummerFile(props.id,row("file").str)}",
                span(
                  em(className := "fa fa-download")
                )
              )
            )
      }

      def refreshBasicData = {
        val map = SeqMap("file" -> "File", "description" -> "Description")
        val basicColumns = map.map { case (k, v) =>
          val title = v match {
            case _ => v
          }
          val fmt = FTool.tbFmt(v)
          val comVF = FTool.comVFmt(v)
          Column(
            field = k,
            title = title,
            formatter = fmt,
            comVF = comVF
          )
        }.toList
        val operateColumn =
          Column(field = "operate", title = "Download", formatter = FTool.operateFmt, comVF = FTool.comVFmt(""))
        val curColumns = basicColumns :+ operateColumn
        val datas = List(
          ujson.Obj("file" -> s"${props.id}.cds.fa", "description" -> "CDS Sequence file"),
          ujson.Obj("file" -> s"${props.id}.pep.fa", "description" -> "Protein Sequence file"),
          ujson.Obj("file" -> s"${props.id}.genome.fa", "description" -> "Genome Sequence file")
        )
        setColumns(curColumns) >>
          setDatas(datas)
      }

    }

    useEffect(
      {
        FTool.refreshBasicData
      },
      List(props.id)
    )

    div(
      h4("Sequence"),
      BootstrapTableCp
        .Props(
          columns = columns,
          datas = datas
        )
        .render
    )

  }

  def apply(id: String) = {
    Component(Props(id))
  }

}
